package top.lyunk.demo.kafka;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/**
 * 普通生产者
 */
public class CommonProducer implements top.lyunk.demo.kafka.Producer {
    Producer<String, String> producer;

    public CommonProducer() {
        Properties props = new Properties();
        props.put(ProducerConfig."bootstrap.servers", "localhost:9092");
        props.put("linger.ms", 1);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        this.producer = new KafkaProducer<>(props);
    }

    @Override
    public void send(String topic, String key, String value) {
        this.producer.send(new ProducerRecord<>(topic, key, value));
    }

    @Override
    public void close() {
        this.producer.close();
    }
}
